package com.leetcode.May;

/**
 * @Description: 和为k的子数组
 * @Author: Mr.Fxy
 * @CreateTime: 2024-05-17 08:07
 */
public class SumK {
    public static void main(String[] args) {
        System.out.println(subarraySum(new int[]{1,2,3}, 3));
    }
    public static int subarraySum(int[] nums, int k) {
        int count = 0;
        for (int start = 0; start < nums.length; ++start) {
            int sum = 0;
            for (int end = start; end >= 0; --end) {
                sum += nums[end];
                if (sum == k) {
                    count++;
                }
            }
        }
        return count;
    }

//    public static int subarraySum(int[] nums, int k) {
//        if (nums[0] == k && nums.length == 1) return 1;
//        int left = 0;
//        int right = nums.length-1;
//        int count = 0;
//        while (left < right) {
//            if (nums[left] + nums[left + 1] == k || nums[left] == k || nums[left+1] == k) {
//                count++;
//                left++;
//            }
//        }
//        return count;
//    }
}
